<?php

class AgentEditForm extends BaseForm {

    public function configure() {
        
        $agent = tblUsersTable::getInstance()->findByDql('id=?', $this->getOption('agent_id'))->getFirst();
        $country = $this->getCountryName($agent->getCountry());
        $region = $this->getRegionName($agent->getRegion());
        $city = $this->getCityName($agent->getCity());
        $time_in = explode(':', $agent->getWorkingTimeFrom());
        $time_out = explode(':', $agent->getWorkingTimeTo());
        
        $this->setWidgets(array(
            'agent_id' => new sfWidgetFormInput(array('default' => $agent->getId()), array('readonly' => true)),
            'name_en' => new sfWidgetFormInput(array('default' => $agent->getTitle(), 'label' => 'Title in English'), array('placeholder' => 'English Name')),
            'name_he' => new sfWidgetFormInput(array('default' => $agent->getTitleHe(), 'label' => 'Title in Hebrew'), array('placeholder' => 'Hebrew Name')),
            'name_ru' => new sfWidgetFormInput(array('default' => $agent->getTitleRu(), 'label' => 'Title in Russian'), array('placeholder' => 'Russian Name')),
            'country' => new sfWidgetFormInput(array('default' => $country), array('placeholder' => 'Country')),
            'region' => new sfWidgetFormInput(array('default' => $region), array('placeholder' => 'Region')),
            'city' => new sfWidgetFormInput(array('default' => $city), array('placeholder' => 'City')),
            'address' => new sfWidgetFormInput(array('default' => $agent->getAddress()), array('placeholder' => 'Address(English)')),
            'time_open' => new sfWidgetFormTime(array('default' => array('hour' => (int)$time_in[0], 'minute' => (int)$time_in[1]))),
            'time_close' => new sfWidgetFormTime(array('default' => array('hour' => (int)$time_out[0], 'minute' => (int)$time_out[1]))),
            'phone1' => new sfWidgetFormInput(array('default' => $agent->getPhone1()), array('placeholder' => 'Phone')),
            'phone2' => new sfWidgetFormInput(array('default' => $agent->getPhone2()), array('placeholder' => 'Phone')),
            'fax' => new sfWidgetFormInput(array('default' => $agent->getFax()), array('placeholder' => 'Fax')),
            'email' => new sfWidgetFormInput(array('default' => $agent->getEmail()), array('placeholder' => 'E-mail')),
            'skype' => new sfWidgetFormInput(array('default' => $agent->getScype()), array('placeholder' => 'Skype')),
            'website' => new sfWidgetFormInput(array('default' => $agent->getWebsite()), array('placeholder' => 'Web-site')),
            'description_en' => new sfWidgetFormTextarea(array('default' => $agent->getDescriptionEn())),
            'description_he' => new sfWidgetFormTextarea(array('default' => $agent->getDescriptionEn())),
            'description_ru' => new sfWidgetFormTextarea(array('default' => $agent->getDescriptionEn())),
            'logo' => new sfWidgetFormInputFile(),
        ));

        $this->widgetSchema->setNameFormat('agentsedit[%s]');

        $this->setValidators(array(
            'logo' => new sfValidatorFile(array(
                'required' => false,
                'mime_types' => array('image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png', 'image/gif'),
                'path' => sfConfig::get('sf_upload_dir') . '/')),
            'agent_id' => new sfValidatorString(array('required' => false)),
            'name_en' => new sfValidatorString(array('required' => false)),
            'name_he' => new sfValidatorString(array('required' => false)),
            'name_ru' => new sfValidatorString(array('required' => false)),
            'country' => new sfValidatorString(array('required' => false)),
            'region' => new sfValidatorString(array('required' => false)),
            'city' => new sfValidatorString(array('required' => false)),
            'address' => new sfValidatorString(array('required' => false)),
            'phone1' => new sfValidatorString(array('required' => false)),
            'phone2' => new sfValidatorString(array('required' => false)),
            'fax' => new sfValidatorString(array('required' => false)),
            'email' => new sfValidatorEmail(array('required' => false)),
            'skype' => new sfValidatorString(array('required' => false)),
            'website' => new sfValidatorString(array('required' => false)),
            'description_en' => new sfValidatorString(array('required' => false)),
            'description_he' => new sfValidatorString(array('required' => false)),
            'description_ru' => new sfValidatorString(array('required' => false)),
            'time_open' => new sfValidatorTime(array('required' => false)),
            'time_close' => new sfValidatorTime(array('required' => false)),
        ));
    }
    
    public function getCountryName($country) {
        $c = tblCountryTable::getInstance()->findByDql('id=?', $country)->getFirst();
        if ($c) {
            return $c->getNameEn();
        } else {
            return null;
        }
    }
    
    public function getRegionName($region) {
        $c = tblRegionTable::getInstance()->findByDql('id=?', $region)->getFirst();
        if ($c) {
            return $c->getNameEn();
        } else {
            return null;
        }
    }
    
    public function getCityName($city) {
        $c = tblCityTable::getInstance()->findByDql('id=?', $city)->getFirst();
        if ($c) {
            return $c->getNameEn();
        } else {
            return null;
        }
    }

}
